import request from '@/utils/http'

// 表选项信息
export interface TableOptionInfo {
    value: string        // 表名（实际值）
    label: string        // 显示名称（表名+注释）
    comment?: string     // 表注释
}

// 表基本信息
export interface TableBasicInfo {
    table_name: string
    table_comment?: string
    table_type?: string
    engine?: string
    table_rows?: number
    data_length?: number
    index_length?: number
    create_time?: string
    update_time?: string
}

// 表结构信息
export interface TableStructureInfo {
    table_name: string
    table_comment?: string
    columns: TableColumnInfo[]
    indexes?: TableIndexInfo[]
    foreign_keys?: TableForeignKeyInfo[]
    table_stats?: TableStatsInfo
}

// 表列信息
export interface TableColumnInfo {
    column_name: string
    data_type: string
    is_nullable: boolean
    column_default?: string
    column_comment?: string
    character_maximum_length?: number
    numeric_precision?: number
    numeric_scale?: number
    is_primary_key?: boolean
    is_unique?: boolean
    is_indexed?: boolean
    ordinal_position: number
}

// 表索引信息
export interface TableIndexInfo {
    index_name: string
    column_name: string
    is_unique: boolean
    index_type?: string
    index_comment?: string
}

// 表外键信息
export interface TableForeignKeyInfo {
    constraint_name: string
    column_name: string
    referenced_table: string
    referenced_column: string
}

// 表统计信息
export interface TableStatsInfo {
    table_rows?: number
    data_length?: number
    index_length?: number
    auto_increment?: number
}

// 表列表响应
export interface TableListResponse {
    total: number
    items: TableBasicInfo[]
    page: number
    size: number
    pages: number
}

// 数据库概要信息
export interface DatabaseSummaryInfo {
    total_tables: number
    total_columns: number
    total_indexes: number
    database_size: number
    table_types: Record<string, number>
}

// 表搜索请求
export interface TableSearchRequest {
    page?: number
    size?: number
    keyword?: string
    table_type?: string
}

// 表列搜索请求
export interface TableColumnSearchRequest {
    table_name: string
    keyword?: string
}

/**
 * 获取表列表（分页）
 */
export function fetchTableList(params: TableSearchRequest) {
    return request.get<TableListResponse>({
        url: '/system/tables/list',
        params
    })
}

/**
 * 获取表结构详情
 */
export function fetchTableStructure(tableName: string) {
    return request.get<TableStructureInfo>({
        url: `/system/tables/structure/${tableName}`
    })
}

/**
 * 获取表列信息
 */
export function fetchTableColumns(tableName: string, keyword?: string) {
    return request.get<TableColumnInfo[]>({
        url: `/system/tables/columns/${tableName}`,
        params: { keyword }
    })
}

/**
 * 搜索表名
 */
export function searchTables(keyword: string) {
    return request.get<TableOptionInfo[]>({
        url: '/system/tables/search',
        params: { keyword }
    })
}

/**
 * 获取数据库概要信息
 */
export function fetchDatabaseSummary() {
    return request.get<DatabaseSummaryInfo>({
        url: '/system/tables/summary'
    })
}

/**
 * 获取所有表名
 */
export function fetchAllTables() {
    return request.get<TableOptionInfo[]>({
        url: '/system/tables/all'
    })
}